<?php

require_once("upload_class_api.php");

class image_upload_api extends file_upload {
		
	var $x_size;
	var $y_size;
	var $x_max_size;
	var $y_max_size;
	var $x_max_thumb_size;
	var $y_max_thumb_size;
	
	var $x_real_size;
	var $y_real_size;
	
	var $thumb_folder;
	var $foto_folder;
	
	var $larger_dim;
	var $larger_curr_value;
	var $larger_dim_value;
	var $larger_dim_thumb_value;
	
	function process_image($create_thumb = false, $delete_tmp_file = false, $compression = IMAGE_COMPRESSION) 
	{		
		$filename = $this->upload_dir.$this->file_copy;
		
		$this->check_dir($this->thumb_folder);
		$this->check_dir($this->foto_folder);
		
		$thumb = $this->thumb_folder.$this->file_copy;
		$foto = $this->foto_folder.$this->file_copy;
			
		$this->check_dimensions($filename);
		
		if ($this->larger_curr_value > $this->larger_dim_value) {
			$this->thumbs($filename, $foto, $this->larger_dim_value, $compression);
		} else {
			copy($filename, $foto);
		}
		
		// real image size
		$img_size = getimagesize($foto);
		$this->x_real_size = $img_size[0];
		$this->y_real_size = $img_size[1];
				
		if ($create_thumb) {
			if ($this->larger_curr_value > $this->larger_dim_thumb_value) {
				$this->icons($filename, $thumb, $this->larger_dim_thumb_value, $compression);
			} else {
				copy($filename, $thumb);
			}
		}
	
		
		if ($delete_tmp_file) $this->del_temp_file($filename);
		
		return true;
	}
	
	function get_img_size($file) {
		$img_size = getimagesize($file);
		$this->x_size = $img_size[0];
		$this->y_size = $img_size[1];
	}
	
	function check_dimensions($filename) {
		$this->get_img_size($filename);
		$x_check = $this->x_size - $this->x_max_size;
		$y_check = $this->y_size - $this->y_max_size;
		if ($x_check < $y_check) {
			$this->larger_dim = "y";
			$this->larger_curr_value = $this->y_size;
			$this->larger_dim_value = $this->y_max_size;
			$this->larger_dim_thumb_value = $this->y_max_thumb_size;
		} else {
			$this->larger_dim = "x";
			$this->larger_curr_value = $this->x_size;
			$this->larger_dim_value = $this->x_max_size;
			$this->larger_dim_thumb_value = $this->x_max_thumb_size;
		}
	}
	
	function img_rotate($wr_file, $comp) {
		$new_x = $this->y_size;
		$new_y = $this->x_size;
		
		$src_img = imagecreatefromjpeg($wr_file);
		$rot_img = imagerotate($src_img, 90, 0);
		$new_img = imagecreatetruecolor($new_x, $new_y);	
		imagecopyresampled($new_img, $rot_img, 0, 0, 0, 0, $new_x, $new_y, $new_x, $new_y);
		imagejpeg($new_img, $this->upload_dir.$this->file_copy, $comp);
	}
		
	function icons($file_name_src, $file_name_dest, $target_size, $quality)
	{
		$size = getimagesize($file_name_src);
		
		$dest = imagecreatetruecolor($target_size, $target_size);
		$file_extension = strtolower(strstr(basename($file_name_src), '.'));
		
		switch($file_extension)
		{
			case '.jpg':
			case '.jpeg':
				$src = imagecreatefromjpeg($file_name_src);
				break;
				
			case '.png':
				$src = imagecreatefrompng($file_name_src);
				break;
				
			case '.bmp':
				$src = imagecreatefromwbmp($file_name_src);
				break;
		}
		
		if($size[0] < $size[1])
		{
			$ratio = $target_size / $size[0];			
			
			if($size[1] * $ratio < $target_size) {
				$new_height = $target_size;
				$new_width = floor(($new_height / $size[1]) * $size[0]);
			}
			else {							
				$new_width = $target_size;
				$new_height = floor(($new_width / $size[0]) *  $size[1]);
			}				
		}
		else
		{
			$ratio = $target_size / $size[1];
			
			if($size[0] * $ratio < $target_size) {
				$new_width = $target_size;
				$new_height = floor(($new_width / $size[0]) *  $size[1]);
			}
			else {
				$new_height = $target_size;
				$new_width = floor(($new_height / $size[1]) * $size[0]);
			}
		}
		
		imagecopyresampled($dest, $src, ($target_size - $new_width) / 2, ($target_size - $new_height) / 2, 0, 0, $new_width, $new_height, $size[0], $size[1]);					
		imagejpeg($dest, $file_name_dest, $quality);
	}
	
	function thumbs($file_name_src, $file_name_dest, $target_size, $quality) 
	{				
		$size = getimagesize($file_name_src);
		
		if ($this->larger_dim == "x") {
			$w = number_format($target_size, 0, ',', '');
			$h = number_format(($size[1]/$size[0])*$target_size,0,',','');
		} else {
			$h = number_format($target_size, 0, ',', '');
			$w = number_format(($size[0]/$size[1])*$target_size,0,',','');
		}

		$dest = imagecreatetruecolor($w, $h);		
		
		$file_extension = strtolower(strstr(basename($file_name_src), '.'));

		switch($file_extension)
		{
			case '.jpg':
			case '.jpeg':
				$src = imagecreatefromjpeg($file_name_src);
				break;
				
			case '.png':
				$src = imagecreatefrompng($file_name_src);
				break;
				
			case '.bmp':
				$src = imagecreatefromwbmp($file_name_src);
				break;
		}

		imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
		imagejpeg($dest, $file_name_dest, $quality);		
	}
}

?>